<template>
    <div>
        <!-- 头部 -->
        <div class="Stop">
            <div class="StopO">
                <img src="../../assets/user/阿拉灯.png" alt="logo">
                <input @click="search()" class="StopOn" type="text" value="输入商品关键词">
                <img class="StopOne" src="../../assets/user/top-search.png" alt="搜索">
                <img class="Stopmsg" src="../../assets/user/top-msg.png" alt="信息" @click="tomsg()">
            </div> 
            <ul class="Stoplist">
                <template v-for="(item,index) in topname"  >
                    <li :key="index" @click="changehuan(index)" :class="{'active':toptr[index]}" >{{item}}</li>
                </template>
            </ul>
        </div>
    </div>
</template>

<script>
export default {
    name:'Top',
    data(){
        return{
            topname:['推荐','贼惦记','美食','母婴'],
            toptr:[false,false,false,false]
        }
    },
    methods:{
        changehuan(v){
            for (let i = 0; i < this.topname.length; i++) {
                this.$set(this.toptr,i,false);
            }
            if(v==3){
                this.$router.replace('/milk');
            }else if(v==0){
                this.$router.replace('/');
            }else if(v==1){
                this.$router.replace('/thoughts');
            }else if(v==2){
                this.$router.replace('/food');
            }
            this.$set(this.toptr,v,true);
        },
        search(){
            this.$router.replace('/search');
        },
        tomsg(){
             this.$router.replace('/msg');
        }
    },
    mounted(){
        var href = window.location.href.split('#')[1];
        switch (href) {
            case '/':
                this.$set(this.toptr,0,true);
                break;
            case '/thoughts':
                this.$set(this.toptr,1,true);
                break;
            case '/food':
                this.$set(this.toptr,2,true);
                break;
            case '/milk':
                this.$set(this.toptr,3,true);
                break;
        }
    }
}
</script>

<style>
/* 头部 */
.Stop {
    background-color: #e53e42;
    width: 100%;
    height: 9.6rem;
    color: #fff;
    font-size: 3.6rem;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10;
}
.StopO {
    height:9.6rem;
    padding: 0 2rem;
    line-height: 9.6rem;
    text-align: left;
}
.StopOn {
    margin-left: 6.7rem;
    height: 4.8rem;
    width: 31.8rem;
    border-radius: 1rem;
    vertical-align: middle;
    margin-bottom: 1rem;
    padding-left: 8.2rem;
    font-size: 2.4rem;
    color: #e83d41;
}
.StopOne {
    position: absolute;
    z-index: 2;
    top: 50%;
    margin-top: -1.5rem;
    left: 23.6rem;
}
.Stopmsg {
    position: absolute;
    right: 2rem;
    top: 50%;
    margin-top: -1.9rem;
}
.Stoplist {
    background: #fff;
    color: #333333;
    display: flex;
    padding: 0 2rem;
    justify-content: space-around;
    font-size: 3rem;
    height: 6rem;
    line-height: 6rem;
    border-bottom: 1px solid #ccc;
}
.active {
    color: #e83d41;
    border-bottom: 0.2rem solid #e83d41;
}
</style>